package com.toolsder.demo.spring.security.jwt.service.impl;

import com.toolsder.demo.spring.security.jwt.entity.ResourcesVO;
import com.toolsder.demo.spring.security.jwt.entity.database.Resources;
import com.toolsder.demo.spring.security.jwt.entity.database.Role;
import com.toolsder.demo.spring.security.jwt.entity.database.User;
import com.toolsder.demo.spring.security.jwt.repository.ResourcesRepository;
import com.toolsder.demo.spring.security.jwt.repository.UserRepository;
import com.toolsder.demo.spring.security.jwt.service.ResourcesService;
import com.toolsder.demo.spring.security.jwt.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * created by 猴子请来的逗比 On 2020/8/11
 *
 * @author by 猴子请来的逗比
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class ResourcesServiceImpl implements ResourcesService {
    @Autowired
    private ResourcesRepository resourcesRepository;

    @Autowired
    private UserRepository userRepository;


    @Autowired
    private SnowFlake snowFlake;

    @Override
    public List<Resources> findAll() {
        return resourcesRepository.findAll();
    }

    @Override
    public List<String> findByPhoneNumber(String username) {
        User user = userRepository.findByUsername(username);
        //获取权限
        Set<Role> gjg2Roles = user.getRoles();
        //加载资源
        List<String> authorties = new ArrayList<>();
        for (Role role : gjg2Roles) {
            Set<Resources> scbz2Resources = role.getResources();
            scbz2Resources.forEach(in -> authorties.add(in.getResourcesUrl()));
        }
        return authorties;
    }

    @Override
    public Resources findByResourcesUrl(String url) {
        return resourcesRepository.findByResourcesUrl(url);
    }

    /**
     * 资源是否存在
     *
     * @param url
     */
    @Override
    public boolean existsByResourcesUrl(String url) {
        return resourcesRepository.existsByResourcesUrl(url);
    }

    /**
     * 保存资源
     *
     * @param url
     * @param name
     */
    @Override
    public Resources save(String url, String name) {
        Resources resources = new Resources();
        resources.setId(snowFlake.nextId());
        resources.setResourcesName(name);
        resources.setResourcesUrl(url);
        resourcesRepository.save(resources);
        return resources;
    }

    /**
     * 更新资源名称
     *
     * @param url
     * @param name
     */
    @Override
    public Resources updateName(String url, String name) {
        Resources resources = resourcesRepository.findByResourcesUrl(url);
        resourcesRepository.updateName(resources.getId(), name);
        return resources;
    }


    /**
     * 修改资源名称
     *
     * @param id
     * @param resourcesName
     */
    @Override
    public void modifyResourcesName(Long id, String resourcesName) {
        //获取资源
        Optional<Resources> resourcesOptional = resourcesRepository.findById(id);
        if (resourcesOptional.isPresent()) {
            Resources resources = resourcesOptional.get();
            resources.setResourcesName(resourcesName);
            resourcesRepository.save(resources);
        }
    }


    private ResourcesVO toVO(Resources resources) {
        return new ResourcesVO(resources);
    }

    private List<ResourcesVO> toVO(List<Resources> resources) {
        List<ResourcesVO> resourcesVOS = new ArrayList<>();
        for (Resources resource : resources) {
            resourcesVOS.add(this.toVO(resource));
        }
        return resourcesVOS;
    }
}
